using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;

namespace StarTrek
{
    static class Display
    {
        #region Variables
        public static List<string> backgroundImageFileNames;    // filenames that will be displayed in the background
        public static int currentImageIndex;                    // the current image being displayed
        public static List<string> imageExtensions = new List<string>(new string[] { "*.bmp", "*.gif", "*.png", "*.jpg", "*.jpeg" });

        // Text that will be written to the scren
        public static Image MessageBackground = null;           
        public static Image MessageImage = null;
        public static string MessageTitle = "";
        public static List<string> MessageBody = new List<string>();

        public static string debugString = "";

        public static TVClient tvClient;
        #endregion

        /// <summary>
        /// Load up images from the picture folder and reset the index to 0
        /// </summary>
        public static void LoadBackgroundImage()
        {
            // Initialize the background images.
            backgroundImageFileNames = new List<string>();
            currentImageIndex = 0;

            LoadImagesFromFolder();
        }

        /// <summary>
        /// Look through the background images folder and store the filenames.
        /// Randomly sort the filenames. The program will display each image in the 
        /// randomly sorted list and then reload once the end is reached. This will
        /// grab any new images that have been added to the folder at runtime.
        /// </summary>
        private static void LoadImagesFromFolder()
        {
            DirectoryInfo backgroundImageDir = new DirectoryInfo(Properties.Settings.Default.BackgroundImagePath);
            backgroundImageFileNames.Clear();

            // For each image extension (.jpg, .bmp, etc.)
            foreach (string imageExtension in imageExtensions)
            {
                // For each file in the directory provided by the user
                foreach (FileInfo file in backgroundImageDir.GetFiles(imageExtension))
                {
                    // Try to load the image
                    try
                    {
                        backgroundImageFileNames.Add(file.FullName);
                    }
                    catch (OutOfMemoryException)
                    {
                        // If the image can't be loaded, move on.
                        continue;
                    }
                }
            }

            // randomly sort the files
            Random r = new Random();
            for (int i = 0; i < backgroundImageFileNames.Count; i++)
            {
                int newindex = r.Next(backgroundImageFileNames.Count);
                string temp = backgroundImageFileNames[i];
                backgroundImageFileNames[i] = backgroundImageFileNames[newindex];
                backgroundImageFileNames[newindex] = temp;
            }
        }

        /// <summary>
        /// Show the next background image or reload if the end has been reached.
        /// </summary>
        public static void NextBackgroundImage()
        {
            // Change the background image to the next image.
            currentImageIndex++;
            if (currentImageIndex >= backgroundImageFileNames.Count)
            {
                currentImageIndex = 0;
                LoadImagesFromFolder();
            }
        }

        public static void Waiting()
        {
            MessageBody.Add("Gathering information...");
        }

        public static void ClearMessageBody()
        {
            MessageBody.Clear();
        }

        /// <summary>
        /// Clear all the text and make sure that the TV client has stopped.
        /// </summary>
        public static void ClearMessage()
        {
            MessageBackground = null;
            MessageTitle = "";
            MessageBody.Clear();
            MessageImage = null;

            if (tvClient != null)
            {
                // Make sure to let the server know that we're done!
                tvClient.StopClient();
                tvClient = null;
            }
        }

        public static void StartTVChannel(string channelName, int channelNumber)
        {
            tvClient = new TVClient();
            tvClient.StartChannel(channelNumber);
        }
    }
}
